package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.LookDetailsBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/3/21.
 */
public class GDetialsAdapter extends BaseAdapter {
    private Context context;
    private List<LookDetailsBean.InfoEntity.CateInfoEntity> cateList;
    private View.OnClickListener listener;
    private List<LookDetailsBean.InfoEntity.CateInfoEntity.DataEntity> dataList = new ArrayList<>();
    private GLookDetialsAdapter mAdapter;
    private ListView my_listView;

    public GDetialsAdapter(Context context, List<LookDetailsBean.InfoEntity.CateInfoEntity> cateList, View.OnClickListener listener, ListView my_listView) {
        this.context = context;
        this.cateList = cateList;
        this.listener = listener;
        this.my_listView = my_listView;
    }

    @Override
    public int getCount() {
        return cateList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SingleAllocationViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new SingleAllocationViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.single_all_item, null);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_all_number = (TextView) convertView.findViewById(R.id.tv_all_number);
            viewHolder.sm_list = (SwipeMenuListView) convertView.findViewById(R.id.sm_list);
            viewHolder.tv_add = (TextView) convertView.findViewById(R.id.tv_add);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (SingleAllocationViewHolder) convertView.getTag();
        }

        viewHolder.tv_name.setText(cateList.get(position).getCate_name() + "");
        viewHolder.tv_all_number.setText("共" + cateList.get(position).getData().size() + "件");

        dataList = cateList.get(position).getData();


        mAdapter = new GLookDetialsAdapter(context, dataList);



        viewHolder.sm_list.setAdapter(mAdapter);
        subject(viewHolder.sm_list, position);
        viewHolder.tv_add.setText("添加"+cateList.get(position).getCate_name() + "");
        viewHolder.tv_add.setOnClickListener(listener);
        viewHolder.tv_add.setTag(position);


        return convertView;
    }

    class SingleAllocationViewHolder {
        TextView tv_name;
        TextView tv_all_number;
        SwipeMenuListView sm_list;
        TextView tv_add;
    }

    public static Paint paint;

    public static float measureTextLength(TextView tv) {
        if (paint == null) {
            paint = new Paint();
        }
        paint.setTextSize(tv.getTextSize());
        return paint.measureText(tv.getText() + "");
    }

    private void subject(final SwipeMenuListView sm_list, final int max_position) {
        my_listView.requestDisallowInterceptTouchEvent(true);

        SwipeMenuCreator creater = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {

                // create置顶item
                SwipeMenuItem item1 = new SwipeMenuItem(context);
                // set item background
                item1.setBackground(new ColorDrawable(0xff008c99));
                // set item width
                item1.setWidth(dp2px(90));
                // set item title
                item1.setTitle("替换");
                // set item title fontsize
                item1.setTitleSize(18);
                // set item title font color
                item1.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(item1);

                //同理create删除item
                SwipeMenuItem deleteItem = new SwipeMenuItem(context);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));

                deleteItem.setTitle("删除");
                deleteItem.setTitleSize(18);
                deleteItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(deleteItem);
            }
        };

        sm_list.setMenuCreator(creater);
        sm_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            }
        });
        sm_list.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                if (myMenuItemClickListener != null) {
                    myMenuItemClickListener.onMenuClick(sm_list, mAdapter, max_position, position, menu, index);

                }
                return true;
            }
        });
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }

    private MyMenuItemClickListener myMenuItemClickListener;

    public void setMyMenuItemClickListener(MyMenuItemClickListener myMenuItemClickListener) {
        this.myMenuItemClickListener = myMenuItemClickListener;
    }

    public interface MyMenuItemClickListener {
        void onMenuClick(SwipeMenuListView sm_list, GLookDetialsAdapter mAdapter, int max_position
                , int position, SwipeMenu menu, int index);
    }
}